package com.foreknow.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.config.CommonPage;
import com.foreknow.entity.User;
import com.foreknow.service.UserServie;
import com.foreknow.utils.Constance;
import com.foreknow.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;

@Api(tags = "UserController", description = "用户登录和展示")
@RestController
@RequestMapping("/user")
public class UserController{
  @Autowired
  private UserServie userServie;

  @ApiOperation("登录")
  @PostMapping("/login")
  public String login(String userName, String password ) throws JsonProcessingException {
    User user = userServie.login(userName,password);
    HashMap<String,Object> hs=new HashMap<>();
    if(user!=null){
      String token= TokenUtil.sign(user);
      hs.put("token",token);
      hs.put("data",user);
      hs.put("meta",new Constance("登录成功!",200));
    } else{
      hs.put("meta",new Constance("用户名或密码错误!",201));
    }
    ObjectMapper objectMapper=new ObjectMapper();
    return objectMapper.writeValueAsString(hs);
  }

  @ApiOperation("用户列表")
  @PostMapping("/getUserList")
  public CommonPage<User> getUserList(Integer deptCategoryId, Integer deptId, Integer registLeId,Integer pageNum,Integer pageSize ){
    List<User> list = userServie.getUserList(deptCategoryId,deptId,registLeId,pageNum,pageSize);
    return CommonPage.restPage(list);
  }
}
